package com.shop.www.controller;

import com.shop.www.model.*;
import com.shop.www.service.CartItemservice;
import com.shop.www.service.Orderservice;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

public class Ordercontroller {
    private Orderservice orderservice;
    private CartItemservice cartItemservice;


    public String checkout(Integer way,HttpSession session){//结账功能
        User user = (User) session.getAttribute("loginuser");
        List<User> sellerlist = cartItemservice.sellerlist(user);
        //遍历购物车中属于不同商家的商品
        for(User seller:sellerlist) {
            //创建订单消息
            Order order = new Order();
            Date now = new Date();
            order.setOrder_No(UUID.randomUUID().toString());
            order.setOrder_date(now);
            order.setUser_ID(user.getUser_ID());
            Cart cart = cartItemservice.getCart(user);
            Map<Integer, CartItem> cartItemMap = cart.getCartItemMap();
            List<CartItem> cartItemList = new ArrayList<CartItem>();
            //把购物车中属于同一个商家的商品取出再封入一个集合
            for (CartItem cartItem : cart.getCartItemMap().values()) {
                if (cartItem.getCommodity().getUser_ID().equals(seller.getUser_ID())) {
                    cartItemList.add(cartItem);}}
            Double totalMoney = 0.00;
            for (CartItem cartItem : cartItemList) {
                totalMoney = totalMoney + cartItem.getCommodity().getPrice() * cartItem.getBuycount();}
            order.setOrder_money(totalMoney);
            Integer totalcount = 0;
            for (CartItem cartItem : cartItemList) {
                totalcount = totalcount + cartItem.getBuycount();}
            order.setOrder_count(cart.getTotalcount());
            order.setOrder_status(way);
            order.setSeller_ID(seller.getUser_ID());
            orderservice.addorder(order,cartItemList);
        }
        return ("finishcheckout");
    }

    public String getOrderList(HttpSession session){//获取全部订单
        User user = (User) session.getAttribute("loginuser");
        List<Order> orderList = orderservice.getorderlistByuser(user);
        user.setOrderList(orderList);
        session.setAttribute("loginuser",user);
        return ("order");
    }

    public String detail(Integer order_ID,HttpSession session){//获取该订单的订单详情
        List<OrderItem> orderItemList = orderservice.getorderItemlist(order_ID);
        session.setAttribute("orderItemList",orderItemList);
        return ("orderItem");
    }

    public String shipped(Integer order_ID){//订单出货
        Order order = new Order(order_ID,2);
        orderservice.updateorder(order);
        return ("redirect:Commodity.do?operate=seller");
    }

    public String takeorder(Integer order_ID, HttpSession session, HttpServletResponse response) throws IOException {//配送员抢单
        User user = (User) session.getAttribute("loginuser");
        Order order = new Order(order_ID,4,user.getUser_ID());
        Order order1 = orderservice.getorder(order_ID);
        if(order1.getOrder_status().equals(4)){
            PrintWriter out = response.getWriter();
            out.println("<script language='javascript'>alert('该订单已被其他配送员抢单');</script>");
            return ("takefailed");
        }else {
            orderservice.updateorder(order);
        }
        return ("redirect:User.do?operate=courier");
    }

    public String finishorder(Integer order_ID,HttpSession session){//客户完成订单
        Order order = new Order(order_ID,5);
        orderservice.finishorder(order);
        User user  = (User)  session.getAttribute("loginuser");
        if(user.getRole().equals(1)){
            return("redirect:order.do?operate=getOrderList");
        }else {
            return ("redirect:User.do?operate=courier");
        }
    }

}
